#include "pch.h"
#include "tmplmailinserttask.h"


CTemplateMailInsertTask::CTemplateMailInsertTask()
:CMysqlTask("TemplateMailInsertTask")
{
}

CTemplateMailInsertTask::~CTemplateMailInsertTask()
{
}

void CTemplateMailInsertTask::Execute(IMysqlConn* poMysqlConn, char* pcBuffer, INT32 nLength)
{
	SetMysqlError(MYSQL_ERR_FAILED);

	std::stringstream ss;
	ss << "insert into mailtemplate(_id, mail) values(" << m_oData.uid();
	ss << ", '";

	std::string strMailData = m_oData.SerializeAsString();
	if(-1 == poMysqlConn->EscapeString(strMailData.c_str(), (INT32)strMailData.size(), pcBuffer, nLength)) return;
	ss << pcBuffer << "')";

	if(!poMysqlConn->Query(ss.str().c_str(), ss.str().size(), NULL))
	{
		GetErrStream() << "ErrDesc: " << poMysqlConn->GetError() << ", ErrNo: " << poMysqlConn->GetErrno();
		return;
	}

	SetMysqlError(MYSQL_ERR_SUCESS);
}

void CTemplateMailInsertTask::OnReply()
{
	if(m_nMysqlErr != MYSQL_ERR_SUCESS)
	{
		LogError("Insert template mail data task failed, id: %llu, errMsg: %s", m_oData.uid(), GetErrStream().str().c_str());
	}
}

CTemplateMailInsertTask2::CTemplateMailInsertTask2()
	:DBTaskBase("TemplateMailInsertTask")
{
}

CTemplateMailInsertTask2::~CTemplateMailInsertTask2()
{
}

// void CTemplateMailInsertTask2::Execute(IMysqlConn* poMysqlConn, char* pcBuffer, INT32 nLength)
// {
// 	SetMysqlError(MYSQL_ERR_FAILED);
// 
// 	std::stringstream ss;
// 	ss << "insert into mailtemplate(_id, mail) values(" << m_oData.uid();
// 	ss << ", '";
// 
// 	std::string strMailData = m_oData.SerializeAsString();
// 	if (-1 == poMysqlConn->EscapeString(strMailData.c_str(), (INT32)strMailData.size(), pcBuffer, nLength)) return;
// 	ss << pcBuffer << "')";
// 
// 	if (!poMysqlConn->Query(ss.str().c_str(), ss.str().size(), NULL))
// 	{
// 		GetErrStream() << "ErrDesc: " << poMysqlConn->GetError() << ", ErrNo: " << poMysqlConn->GetErrno();
// 		return;
// 	}
// 
// 	SetMysqlError(MYSQL_ERR_SUCESS);
// }

// void CTemplateMailInsertTask2::OnReply()
// {
// 	if (m_nMysqlErr != MYSQL_ERR_SUCESS)
// 	{
// 		LogError("Insert template mail data task failed, id: %llu, errMsg: %s", m_oData.uid(), GetErrStream().str().c_str());
// 	}
// }

bool CTemplateMailInsertTask2::db_thread_process()
{
	SetMysqlError(MYSQL_ERR_FAILED);

	std::stringstream ss;
	ss << "insert into mailtemplate(_id, mail) values(" << m_oData.uid();
	ss << ", '";

	std::string strMailData = m_oData.SerializeAsString();
	if (-1 == pdbi_->EscapeString(strMailData.c_str(), (INT32)strMailData.size(), NULL, 0)) return false;
	char* pcBuffer = pdbi_->GetEscapStringBuff();
	ss << pcBuffer << "')";

	if (!pdbi_->Query(ss.str().c_str(), ss.str().size(), NULL))
	{
		GetErrStream() << "ErrDesc: " << pdbi_->GetError() << ", ErrNo: " << pdbi_->GetErrno();
		return false;
	}

	SetMysqlError(MYSQL_ERR_SUCESS);
	
	return false;
}

thread::TPTask::TPTaskState CTemplateMailInsertTask2::presentMainThread()
{
	if (m_nMysqlErr != MYSQL_ERR_SUCESS)
	{
		LogError("Insert template mail data task failed, id: %llu, errMsg: %s", m_oData.uid(), GetErrStream().str().c_str());
	}
	return DBTaskBase::presentMainThread()/*thread::TPTask::TPTASK_STATE_COMPLETED*/;
}